import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;

public class MainMenu extends GameCanvas{
	
    public static final int MENU =0,SET=1,ABOUT=2,HELP=3;
	
    //主类对象
	private LLKMIDlet midlet;
	//标识当前所选择的选项
	private int selected = 0;
    //背景框架
	private Image imgBackground;
	//菜单图片
	private Image imgMenu;
	//关于图片
	private Image imgAbout;
	//设置图片
	private Image imgSet;
	//操作帮助图片
	private Image imgHelp;
	//标识当前难度等级
	private int level = 0;
	//当前选择菜单选项 0是Menu主菜单 1 是设置页面 2是关于页面 3是帮助页面
	private int status = 0;
	
	//绘制笔触
	private Graphics g;
	
	
	public MainMenu(LLKMIDlet midlet) {
		//是否抑制键盘
		super(false);
		//主类
		this.midlet=midlet;
		//将屏幕属性设置为全屏
		setFullScreenMode(true);
		try {
			//加载背景框架图片
			imgBackground = Image.createImage("/background.png");
			//加载菜单图片
			imgMenu = Image.createImage("/menu.png");
			//加载关于图片
			imgAbout = Image.createImage("/about.png");
			//加载设置图片
			imgSet = Image.createImage("/set.png");
			//加载帮助图片
			imgHelp = Image.createImage("/help.png");
		}catch(Exception e) {
			//捕获异常并输出
			e.printStackTrace();
		}
		//获取笔触对象
		g = getGraphics();
		//绘制框架
		g.drawImage(imgBackground,0,0,g.TOP | g.LEFT);
		//绘制菜单
		drawMenu();
		
		
	}
	
	private void drawMenu() {
		//获得笔触
		g = getGraphics();
		//绘制背景图片
		g.drawImage(imgMenu,88,114,g.TOP|g.LEFT);
		
		//设置颜色为红色
		g.setColor(255,0,0);
		
		//绘制红色方框  3层增加颜色深度 相当于3px 的框  而且是往里面写 写一个小框
		g.drawRect(98,114+selected*37,125,39);
		
		//绘制红色方框
		g.drawRect(99,115+selected*37,123,37);
				
		//绘制红色方框
		g.drawRect(100,116+selected*37,121,35);		
		
	
	}
	
                                        
	private void drawSet(){
	    g.drawImage(imgSet, 88, 114, g.TOP | g.LEFT);             //绘制选择难度图片
	    g.setColor(255, 0, 0);                                    //设置颜色为红色
	    g.drawRect(98, 162+level*37, 125, 39);                 //绘制红色方框
	    g.drawRect(99, 163+level*37, 123, 37);                 //绘制红色方框
	    g.drawRect(100, 164+level*37, 121, 35);                //绘制红色方框
	}
	
	
	private void drawHelp(){
	    g.drawImage(imgHelp, 88, 114, g.TOP | g.LEFT);
	}
	private void drawAbout(){
	    g.drawImage(imgAbout, 88, 114, g.TOP | g.LEFT);
	}
	
	public void paint(Graphics g) {
		flushGraphics();
	}
	
	protected void keyPressed(int keyCode) {
		//获取当前按键
		int g  = getGameAction(keyCode);
		if(status == MENU) {
			//处于菜单状态下
			switch(g) {
			
			case UP:
			case LEFT:
				selected--;
				if(selected ==-1) {
					selected = 4;
				}
				drawMenu();
				break;
			case RIGHT:
			case DOWN:
				selected++;
				
				if(selected ==5) {
					selected = 0;
				}
				drawMenu();
				break;
				
			case FIRE:
				//当玩家按下确认键时
				switch(selected) {
				case 0:
					midlet.startGame(level);
					break;
				case 1:
					status = SET;
					drawSet();
				    break;
				case 2:
					status = HELP;
					drawHelp();
				    break;
				case 3:
					status = ABOUT;
					drawAbout();
					break;
					
				case 4:
					midlet.exit();
					break;
				default:
					break;
				}
					
			}
			
			
			//按键完操作后重绘
			repaint();
			return;
		}
		
		if(status == SET) {
			//处于游戏设置状态
			switch(g) {
			
			case UP:
			case LEFT:
				level--;
				if(level == -1) {
					level =2;
				}
				drawSet();
				break;
				
			case RIGHT:
			case DOWN:
				level++;
				if(level ==3 ) {
					level = 0;
				}
				drawSet();
				break;
			case FIRE:
				status = MENU;
				drawMenu();
				
			default:
				break;
			
			
			}
		}
		
		if(status ==ABOUT || status == HELP) {
			//按任意按键返回菜单
			status = MENU;
			drawMenu();
		}
		
		//重新绘制屏幕
		repaint();
	}
	
	

}
